<template>
	<view class="community-page">
		<scroll-view scroll-y class="posts-container" @scrolltolower="loadMore" :show-scrollbar="false">
			<view class="posts-grid">
				<view 
					v-for="post in posts" 
					:key="post.id" 
					class="post-card"
					@click="viewDetail(post.id)"
				>
					<image 
						class="post-image" 
						:src="getImageUrl(post.imageUrl)" 
						mode="aspectFill" 
					/>
					<view class="post-info">
						<text class="post-title">{{ post.title }}</text>
						<view class="post-meta">
							<text class="post-author">{{ post.authorName }}</text>
							<text class="post-time">{{ formatTime(post.createdAt) }}</text>
						</view>
					</view>
				</view>
			</view>
			<view v-if="loading" class="loading">加载中...</view>
			<view v-if="!hasMore && posts.length > 0" class="no-more">没有更多了</view>
		</scroll-view>
		
		<view class="fab" @click="showCreatePost">
			<text class="fab-icon">+</text>
		</view>
	</view>
</template>

<script>
import { api } from '../../utils/request.js'
import { BASE_URL } from '../../utils/server.js'

export default {
	data() {
		return {
			posts: [],
			pageNum: 1,
			pageSize: 20,
			loading: false,
			hasMore: true
		}
	},
	onLoad() {
		this.loadPosts()
	},
	onShow() {
		// 从发帖页面返回时刷新列表
		this.refresh()
	},
	methods: {
		async loadPosts(reset = false) {
			if (this.loading) return
			if (!this.hasMore && !reset) return
			
			this.loading = true
			try {
				const res = await api.getPosts({
					pageNum: reset ? 1 : this.pageNum,
					pageSize: this.pageSize
				})
				
				if (res.code === 0) {
					const data = res.data
					const newPosts = data.records || data.list || []
					
					if (reset) {
						this.posts = newPosts
						this.pageNum = 2
					} else {
						this.posts = [...this.posts, ...newPosts]
						this.pageNum++
					}
					
					this.hasMore = newPosts.length >= this.pageSize
				} else {
					uni.showToast({ icon: 'none', title: res.message || '加载失败' })
				}
			} catch (e) {
				uni.showToast({ icon: 'none', title: '加载失败' })
			} finally {
				this.loading = false
			}
		},
		loadMore() {
			if (!this.loading && this.hasMore) {
				this.loadPosts()
			}
		},
		refresh() {
			this.pageNum = 1
			this.hasMore = true
			this.loadPosts(true)
		},
		showCreatePost() {
			uni.navigateTo({
				url: '/pages/community/create'
			})
		},
		viewDetail(id) {
			uni.navigateTo({
				url: `/pages/community/detail?id=${id}`
			})
		},
		getImageUrl(url) {
			if (!url) return '/static/logo.png'
			if (url.startsWith('http')) return url
			return BASE_URL + url
		},
		formatTime(timeStr) {
			if (!timeStr) return ''
			const date = new Date(timeStr)
			const now = new Date()
			const diff = now - date
			
			if (diff < 60000) return '刚刚'
			if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
			if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
			if (diff < 604800000) return Math.floor(diff / 86400000) + '天前'
			
			const year = date.getFullYear()
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			return `${year}-${month}-${day}`
		}
	}
}
</script>

<style>
.community-page {
	position: relative;
	height: 100vh;
	background: #f5f5f5;
}

.posts-container {
	height: 100%;
	padding: 20rpx 20rpx 20rpx 20rpx;
	box-sizing: border-box;
}

.posts-grid {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 20rpx;
}

.post-card {
	background: #fff;
	border-radius: 16rpx;
	overflow: hidden;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
}

.post-image {
	width: 100%;
	height: 300rpx;
	background: #f0f0f0;
}

.post-info {
	padding: 16rpx;
}

.post-title {
	font-size: 28rpx;
	color: #333;
	font-weight: 500;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
	margin-bottom: 12rpx;
}

.post-meta {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 22rpx;
	color: #999;
}

.post-author {
	flex: 1;
}

.post-time {
	flex-shrink: 0;
}

.fab {
	position: fixed;
	bottom: 120rpx;
	left: 50%;
	transform: translateX(-50%);
	width: 100rpx;
	height: 100rpx;
	background: linear-gradient(135deg, #39c5bb, #54e0c7);
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 8rpx 24rpx rgba(57, 197, 187, 0.4);
	z-index: 100;
}
/* #ifdef MP-WEIXIN */
.fab {
	bottom: calc(0rpx + env(safe-area-inset-bottom));
}
/* #endif */

.fab-icon {
	color: #fff;
	font-size: 60rpx;
	font-weight: 300;
	line-height: 1;
}

.loading, .no-more {
	text-align: center;
	padding: 40rpx;
	color: #999;
	font-size: 24rpx;
}
</style>
